package com.yaolaizaixian.merchant.home;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.animation.AlphaAnimation;
import android.widget.LinearLayout;

import com.yaolaizaixian.merchant.HomeActivity;
import com.yaolaizaixian.merchant.LoginActivity;
import com.yaolaizaixian.merchant.R;
import com.yaolaizaixian.merchant.base.BaseActivity;
import com.yaolaizaixian.merchant.utils.SharedPreferencesUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/3/1.
 */
public class SplashActivity extends BaseActivity {


    @BindView(R.id.ll_splash)
    LinearLayout llSplash;
    private long sleepTime=2000;

    @Override
    public void initView() {

        setContentView(R.layout.activity_splash);

    }

    @Override
    public void initData() {


        //渐变动画
        AlphaAnimation animation = new AlphaAnimation(1f, 1.0f);
        animation.setDuration(1500);
        llSplash.startAnimation(animation);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    @Override
    protected void onStart() {
        super.onStart();

        //员工id
        final String usersysno = SharedPreferencesUtil.getString(getApplicationContext(), "usersysno", "");

        new Thread(new Runnable() {
            @Override
            public void run() {

                //员工id
                if (!TextUtils.isEmpty(usersysno)) {

                    // 自动登录，在登录之前
                    long start = System.currentTimeMillis();
                    long costTime = System.currentTimeMillis() - start;
                    //wait
                    if (sleepTime - costTime > 0) {
                        try {

                            Thread.sleep(sleepTime - costTime);

                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                    }
                    //enter main screen
                    startActivity(new Intent(SplashActivity.this, HomeActivity.class));
                    finish();
                }

                else {

                    try {

                        Thread.sleep(sleepTime);
                        startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                        finish();

                    } catch (InterruptedException e) {

                    }

                }
            }
        }).start();

    }
}
